package com.dudu.controller;

import com.dudu.dto.ResultDto;
import com.dudu.pojo.Orders;
import com.dudu.pojo.Services;
import com.dudu.service.OrderService;
import com.dudu.service.ServicesService;
import com.dudu.vo.ConfirmPhoneVo;
import com.dudu.vo.OrderServiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ServicesService servicesService;

    @GetMapping("/tui")
    public ResultDto tui(@RequestParam String orderId) {
       return orderService.tui(orderId);

    }
    @PostMapping("/addOrder")
    public ResultDto addOrder(@RequestBody Orders orders) {
        return orderService.addOrder(orders);
    }

    @GetMapping("/getOrderStatusByOrderId")

    public ResultDto getOrderStatusByOrderId(@RequestParam String orderId) {
//        System.out.println(orderId+"--32");
        // 根据订单id查询订单状态
        Orders orderInfo = orderService.getById((orderId));
        if (orderInfo!=null) {
            if (orderInfo.getStatus() == 1) {
                return ResultDto.success("订单已完成", null);
            }
        }
        return ResultDto.success("订单未完成", null);
    }

    @GetMapping("/getOrderList")
    public ResultDto getOrderList() {
        return ResultDto.success("ok", orderService.getOrderList());
    }

    @PostMapping("/receiveOrder")
    public ResultDto receiveOrder(@RequestBody Services services) {
        services.setCreateTime(new Date());
        orderService.receiveOrder(services);
        return ResultDto.success("接单成功！", null);
    }

    @PostMapping("/confirmPhone")
    public ResultDto confirmPhone(@RequestBody ConfirmPhoneVo confirmPhoneVo) {
        return orderService.confirmPhone(confirmPhoneVo);
    }

    @PostMapping("/getOrder")
    public ResultDto getOrder(@RequestBody Orders orders) {
        return orderService.getOrderById(orders);
    }

    @PostMapping("/updateOrderStatus")
    public ResultDto updateOrderStatus(@RequestBody Orders orders) {
        return orderService.updateOrderStatus(orders);
    }

    @PostMapping("/updateOrderService")
    public ResultDto updateOrderService(@RequestBody OrderServiceVo orderServiceVo) {
        Orders orders = new Orders();
        orders.setOrderId(orderServiceVo.getOrderId());
        orders.setStatus(orderServiceVo.getStatus());
        Services services = new Services();
        services.setOrderId(orderServiceVo.getOrderId());
        services.setStartLatitude(orderServiceVo.getStartLatitude());
        services.setStartLongitude(orderServiceVo.getStartLongitude());
        services.setEndTime(new Date());
        orderService.updateOrderStatus(orders);
        return servicesService.updateOrderService(services);
    }
}
